﻿using System.Linq;
using ZSZ.Application.IService;
using ZSZ.Core.Entities;
using ZSZ.DTO;
using ZSZ.EntityFramework;

namespace ZSZ.Application.Service
{
    public class IdNameService : Repository<IdNameEntity>, IIdNameService
    {
        public long AddNew(string typeName, string name)
        {
            
            IdNameEntity idName =
                new IdNameEntity { Name = name, TypeName = typeName };

            //todo:检查重复性
            SaveOrUpdate(idName,true);
            return idName.Id;
            
        }

        private IdNameDTO ToDTO(IdNameEntity entity)
        {
            IdNameDTO dto = new IdNameDTO
            {
                CreateDateTime = entity.CreateDateTime,
                Id = entity.Id,
                Name = entity.Name,
                TypeName = entity.TypeName
            };
            return dto;
        }

        public IdNameDTO[] GetAll(string typeName)
        {
            return GetAllDataEntity().Where(e => e.TypeName == typeName)
                .ToList().Select(e=>ToDTO(e)).ToArray();
        }

        public IdNameDTO GetById(long id)
        {
            
            return ToDTO(GetOneById(id));
            
        }

        public IdNameService(ZSZDbContext context) : base(context)
        {
        }
    }
}
